package com.example.appointment.controller;

import com.example.appointment.annotation.RequireRole;
import com.example.appointment.dto.ActivityRegistrationDTO;
import com.example.appointment.dto.ClubActivityDTO;
import com.example.appointment.service.ActivityRegistrationService;
import com.example.appointment.service.ClubActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团活动管理控制器（仅管理员可用）
 */
@RestController
@RequestMapping("/api/admin/activities")
public class ClubActivityAdminController {

    @Autowired
    private ClubActivityService activityService;
    
    @Autowired
    private ActivityRegistrationService registrationService;
    
    /**
     * 获取所有活动列表（包括非活跃的）
     */
    @GetMapping
    @RequireRole({"ADMIN"})
    public ResponseEntity<Map<String, Object>> getAllActivities(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "createdAt") String sortBy,
            @RequestParam(defaultValue = "desc") String direction) {
        
        Sort sort = "desc".equalsIgnoreCase(direction) ?
                Sort.by(sortBy).descending() : Sort.by(sortBy).ascending();
        
        Pageable pageable = PageRequest.of(page, size, sort);
        
        // 注意：这里需要在ClubActivityService中添加获取所有活动的方法
        Page<ClubActivityDTO> activitiesPage = activityService.getAllActivities(pageable);
        
        Map<String, Object> response = new HashMap<>();
        response.put("activities", activitiesPage.getContent());
        response.put("currentPage", activitiesPage.getNumber());
        response.put("totalItems", activitiesPage.getTotalElements());
        response.put("totalPages", activitiesPage.getTotalPages());
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 获取活动的报名记录
     */
    @GetMapping("/{id}/registrations")
    @RequireRole({"ADMIN"})
    public ResponseEntity<Page<ActivityRegistrationDTO>> getActivityRegistrations(
            @PathVariable Long id,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        Pageable pageable = PageRequest.of(page, size);
        Page<ActivityRegistrationDTO> registrations = registrationService.getActivityRegistrations(id, pageable);
        
        return ResponseEntity.ok(registrations);
    }
    
    /**
     * 标记用户是否参加了活动
     */
    @PutMapping("/registrations/{registrationId}/attendance")
    @RequireRole({"ADMIN"})
    public ResponseEntity<Void> markAttendance(
            @PathVariable Long registrationId,
            @RequestParam boolean attended) {
        
        registrationService.markAttendance(registrationId, attended);
        return ResponseEntity.ok().build();
    }
} 